package com.zycfc.zsf.boot.util.text;

import java.net.*;
import java.util.*;
import java.nio.file.attribute.*;
import java.nio.file.*;
import java.io.*;
import org.slf4j.*;

public class ResourceLoaderUtils
{
    private static final Logger log;
    
    public static void load(final ResourceLoader resourceLoader, String resourcePaths) {
        resourcePaths = resourcePaths.trim();
        if ("".equals(resourcePaths)) {
            return;
        }
        final List<String> result = new ArrayList<String>();
        for (String resource : resourcePaths.split("[,\uff0c]")) {
            try {
                resource = resource.trim();
                if (resource.startsWith("classpath:")) {
                    result.addAll(loadClasspathResource(resource.replace("classpath:", ""), resourceLoader, resourcePaths));
                }
                else if (resource.startsWith("http:")) {
                    result.addAll(loadHttpResource(resource, resourceLoader));
                }
                else {
                    result.addAll(loadNoneClasspathResource(resource, resourceLoader, resourcePaths));
                }
            }
            catch (Exception e) {
                ResourceLoaderUtils.log.error("\u52a0\u8f7d\u8d44\u6e90\u5931\u8d25\uff1a" + resource, (Throwable)e);
            }
        }
        resourceLoader.clear();
        resourceLoader.load(result);
    }
    
    private static List<String> loadNoneClasspathResource(final String resource, final ResourceLoader resourceLoader, final String resourcePaths) throws IOException {
        final List<String> result = new ArrayList<String>();
        final Path path = Paths.get(resource, new String[0]);
        final boolean exist = Files.exists(path, new LinkOption[0]);
        if (!exist) {
            return result;
        }
        final boolean isDir = Files.isDirectory(path, new LinkOption[0]);
        if (isDir) {
            result.addAll(loadAndWatchDir(path, resourceLoader, resourcePaths));
        }
        else {
            result.addAll(load(resource));
        }
        return result;
    }
    
    private static List<String> loadHttpResource(final String resource, final ResourceLoader resourceLoader) throws MalformedURLException, IOException {
        final List<String> result = new ArrayList<String>();
        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(resource).openConnection().getInputStream(), "utf-8"))) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (!"".equals(line)) {
                    if (line.startsWith("#")) {
                        continue;
                    }
                    result.add(line);
                }
            }
        }
        return result;
    }
    
    private static List<String> loadClasspathResource(final String resource, final ResourceLoader resourceLoader, final String resourcePaths) throws IOException {
        final List<String> result = new ArrayList<String>();
        final Enumeration<URL> ps = ResourceLoaderUtils.class.getClassLoader().getResources(resource);
        while (ps.hasMoreElements()) {
            final URL url = ps.nextElement();
            if (url.getFile().contains(".jar!")) {
                result.addAll(load("classpath:" + resource));
            }
            else {
                final File file = new File(url.getFile());
                final boolean dir = file.isDirectory();
                if (dir) {
                    result.addAll(loadAndWatchDir(file.toPath(), resourceLoader, resourcePaths));
                }
                else {
                    result.addAll(load(file.getAbsolutePath()));
                }
            }
        }
        return result;
    }
    
    private static List<String> loadAndWatchDir(final Path path, final ResourceLoader resourceLoader, final String resourcePaths) {
        final List<String> result = new ArrayList<String>();
        try {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                    result.addAll(load(file.toAbsolutePath().toString()));
                    return FileVisitResult.CONTINUE;
                }
            });
        }
        catch (IOException ex) {
            ResourceLoaderUtils.log.error("\u52a0\u8f7d\u8d44\u6e90\u5931\u8d25\uff1a" + path, (Throwable)ex);
        }
        return result;
    }
    
    private static List<String> load(final String path) {
        final List<String> result = new ArrayList<String>();
        try {
            InputStream in = null;
            if (path.startsWith("classpath:")) {
                in = ResourceLoaderUtils.class.getClassLoader().getResourceAsStream(path.replace("classpath:", ""));
            }
            else {
                in = new FileInputStream(path);
            }
            try (final BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    line = line.trim();
                    if (!"".equals(line)) {
                        if (line.startsWith("#")) {
                            continue;
                        }
                        result.add(line);
                    }
                }
            }
        }
        catch (Exception e) {
            ResourceLoaderUtils.log.error("\u52a0\u8f7d\u8d44\u6e90\u5931\u8d25\uff1a" + path, (Throwable)e);
        }
        return result;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ResourceLoaderUtils.class);
    }
}
